package og.example.views;

import og.example.controller.UserController;

import javax.swing.*;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginFrame extends JFrame {
    private JTextField userNameText;
    private JPasswordField userPasswordText;

    public LoginFrame() {
        // 设置窗口标题
        setTitle("欢迎使用随机点名系统");
        // 设置窗口位置及大小
        setSize(400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel loginPanel = new JPanel();
        JPanel userPanel = new JPanel();
        JPanel passwordPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        loginPanel.setBorder(BorderFactory.createTitledBorder("用户登录"));
        JLabel userNameLabel = new JLabel("用户名：");
        userNameText = new JTextField(20);
        userPanel.add(userNameLabel);
        userPanel.add(userNameText);
        JLabel userPasswordLabel = new JLabel("密   码：");
        userPasswordText = new JPasswordField(20);
        passwordPanel.add(userPasswordLabel);
        passwordPanel.add(userPasswordText);
        JButton loginButton = new JButton("登录");
        JButton registerButton = new JButton("注册");
        buttonPanel.add(loginButton);
        buttonPanel.add(registerButton);
        buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 60, 10));
        loginPanel.add(userPanel);
        loginPanel.add(passwordPanel);
        loginPanel.add(buttonPanel);
        // 设置布局管理器为 FlowLayout
        loginPanel.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));

        // 添加按钮的事件监听器
        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 登录逻辑
                System.out.println("登录按钮被点击");
                String username = userNameText.getText();
                String password = userPasswordText.getText();
                UserController userController = new UserController();
                boolean isLoginSuccess = userController.login(username, password);
                if (isLoginSuccess) {
                    // 登录成功，关闭当前窗口，打开新的窗口
                    dispose();
                    new MainFrame();
                } else {
                    // 登录失败，弹出提示框
                    JOptionPane.showMessageDialog(LoginFrame.this, "用户名或密码错误！如没有账户请先点击注册按钮注册账户", "登录失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });

        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 注册逻辑
                System.out.println("注册按钮被点击");
                String username = userNameText.getText();
                String password = userPasswordText.getText();
                UserController userController = new UserController();
                boolean isRegisterSuccess = userController.register(username, password);
                if (isRegisterSuccess) {
                    JOptionPane.showMessageDialog(LoginFrame.this, "注册成功！", "注册成功", JOptionPane.INFORMATION_MESSAGE);
                }else {
                    JOptionPane.showMessageDialog(LoginFrame.this, "注册失败，用户名已存在！", "注册失败", JOptionPane.ERROR_MESSAGE);
                }
            }
        });


        // 将 loginPanel 添加到 JFrame
        add(loginPanel);

        // 设置窗口可见
        setVisible(true);

    }

}